package com.huawei.chengdu.meetingdemo.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.huawei.chengdu.meetingdemo.R;
import com.huawei.chengdu.meetingdemo.model.AddrBookBean;
import com.huawei.chengdu.meetingdemo.util.LogUtil;
import com.huawei.chengdu.meetingdemo.view.dialogs.InviteSiteDialog;

import java.util.concurrent.CopyOnWriteArrayList;

public class InviteSiteSelectedAdapter extends RecyclerView.Adapter<InviteSiteSelectedAdapter.SiteSelectedViewHolder> {
    private static final String TAG = InviteSiteSelectedAdapter.class.getSimpleName();

    private Context mContext;

    private InviteSiteAdapter inviteSiteAdapter;

    private InviteSiteDialog inviteSiteDialog;

    private CopyOnWriteArrayList<AddrBookBean> selectedSiteList = new CopyOnWriteArrayList<>();

    /**
     * 构造器
     *
     * @param mContext         上下文
     * @param inviteSiteDialog 邀请会场弹框
     */
    public InviteSiteSelectedAdapter(Context mContext, InviteSiteDialog inviteSiteDialog) {
        this.mContext = mContext;
        this.inviteSiteDialog = inviteSiteDialog;
        this.inviteSiteAdapter = inviteSiteDialog.getInviteSiteAdapter();
    }

    public CopyOnWriteArrayList<AddrBookBean> getSelectedSiteList() {
        return selectedSiteList;
    }

    @NonNull
    @Override
    public SiteSelectedViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.invite_site_selected_item_layout, parent, false);
        return new SiteSelectedViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SiteSelectedViewHolder holder, int position) {
        holder.initData(position);
    }

    @Override
    public int getItemCount() {
        inviteSiteDialog.updateSelectedBgr(selectedSiteList.size());
        return selectedSiteList.size();
    }

    /**
     * 清空所有已选
     */
    public void clearSelectedSite() {
        selectedSiteList.clear();
        inviteSiteAdapter.updateData();
        notifyDataSetChanged();
    }

    /**
     * 与会者列表数据VH
     */
    class SiteSelectedViewHolder extends RecyclerView.ViewHolder {
        private ImageView ivSiteIcon;

        private TextView tvSiteName;

        private ImageView ivSelected;

        private AddrBookBean curAddrBean;

        private int position;

        /**
         * 构造器
         *
         * @param itemView itemView
         */
        public SiteSelectedViewHolder(@NonNull View itemView) {
            super(itemView);
            initView(itemView);
        }

        private void initView(View itemView) {
            ivSiteIcon = itemView.findViewById(R.id.iv_site_icon);
            tvSiteName = itemView.findViewById(R.id.tv_site_name);
            ivSelected = itemView.findViewById(R.id.iv_select);
        }

        /**
         * 初始化
         *
         * @param position pos
         */
        public void initData(int position) {
            LogUtil.info(TAG + " initData,pos=" + position);
            this.position = position;
            curAddrBean = selectedSiteList.get(position);
            tvSiteName.setText(curAddrBean.getName());
            ivSiteIcon.setImageResource(curAddrBean.getInviteAvatarId());
            initEvent();
        }

        private void initEvent() {
            ivSelected.setOnClickListener(view -> handleUnSelect());
        }

        private void handleUnSelect() {
            LogUtil.info(TAG + " handleUnSelect,pos=" + position);
            curAddrBean.setChecked(false);
            selectedSiteList.remove(position);

            inviteSiteAdapter.updateData();
            notifyDataSetChanged();
        }
    }
}
